<?php
namespace app\index\controller;

use think\Config;
use think\Cookie;
use think\Request;
use think\Session;

class Shop extends Base
{


    public function index($id='',$p='')
    {
        if(!$id){
            $id = Request::instance()->get('id');
        }
        if($id){
            $shop = model('shop')->where('id',$id)->find();
        }
        else{
            $shop = $this->getMyShop();
        }
        //是否来自派单
        if($p && $p==1){
           Cookie::set('isP',true);
        }
        else{
            Cookie::delete('isP');
        }
        $products = model('product')->getProductByShopId($shop->id);
        $shopComment = model('comment')->getShopComment($shop->id);
        if(!$shopComment){
            $rate = '100%';
        }
        else{
            $goodComment = model('comment')->getShopCommentByType($shop->id,0);
            $rate = 100 * count($goodComment)/count($shopComment);
            $rate = $rate.'%';
        }
        $this->assign('rate',$rate);
        $this->assign('productCount',count($products['products']));
        $this->assign('products',$products);
        $this->assign('shop',$shop);
        $this->assign('key','');
        return view('index');

    }


    public function myShopSet(){
        return view('myShopSet');
    }


    public function saveShopSet(){
        $data = Request::instance()->post();
        $img = Request::instance()->file('img');
        model('shop')->saveShop($this->uid,$data['name'],$data['phone'],$img);
        $this->redirect('center/myShop');
    }




    //所有产品
    public function product(){

        $products = model('product')->allProducts();
        $this->assign('products',$products);
        return view('product');
    }




    public function  addProduct(){
        $this->isCanAddProduct();
        return view('addProduct');
    }


    public function  upProduct($id){
        $this->isCanAddProduct();
        $product = model('product')->where('id',$id)->find();
        $guige = model('product_guige')->where('productId',$id)->select();
        $this->assign('product',$product);
        $this->assign('guige',$guige);
        return view('upProduct');
    }


    public function  saveProduct(){
        $this->isCanAddProduct();
        $shop = $this->getMyShop();
        $data = Request::instance()->post();
        $data['shopId'] = $shop->id;
        $data['uid'] = $this->uid;
        $img = Request::instance()->file('img');
        model('product')->saveProduct($data,$img);
        $this->redirect('center/goodsShelves');
    }


    //下架
    public function sxjProduct($id,$v){
        $this->isCanAddProduct();
        model('product')->changeShow($id,$v);
        $this->redirect('center/goodsShelves');
    }


    public function product_dtl($id,$sid){
        $shop = model('shop')->getHomeShopInfo( $sid);
        $data = model('product')->product_dtl($id, $sid,$this->uid);
        $this->assign('shop',$shop);
        $this->assign('product',$data['product']);
        $this->assign('guige',$data['guige']);
        $this->assign('commentCount',$data['commentCount']);
        $this->assign('rate',$data['rate']);
        $this->assign('isLike',$data['isLike']);
        return view('product_dtl');

    }


    public function addOrCancelLike($productId,$shopId,$uid){

        return model('like')->addOrCancelLike($productId,$shopId,$uid);
    }


    public function buy($id,$sid,$aid='',$gid){
        $product = model('product')->where('id',$id)->find();
        if($aid){
           $address =  model('address')->getAddressById($aid);
        }
        else{
            $address =  model('address')->getUserDefaultAddress($this->uid);
            if(!$address){
                $address =  model('address')->getAUserAddress($this->uid);
            }
        }
        $price = model('product_guige')->where('id',$gid)->value('salePrice');
        $this->assign('address',$address);
        $this->assign('product',$product);
        $this->assign('id',$id);
        $this->assign('sid',$sid);
        $this->assign('gid',$gid);
        $this->assign('price',$price);
        return view('buy');
    }


    public function saveOrder(){
        $data = Request::instance()->post();
        $product = model('product')->where('id',$data['productId'])->find();
        $shop = model('shop')->where('id',$data['shopId'])->find();
        $guige = model('product_guige')->where('id',$data['gid'])->find();
        unset($data['gid']);
        if($product){
            if($product->uid == $shop->owner){
                //自己的商品
                $data['costPrice'] = 0;
            }
            else{
                //代理上级商品
                $data['costPrice'] = $data['num']* 100*$guige->agentPrice; //成本
            }
            $data['buyUid'] = $this->uid;
            $data['ugrade'] = $this->user->grade;
            $data['status'] = 0;
            $data['guige'] = $guige->guige;
            $data['totalFee'] = $data['num']* 100*$guige->salePrice;
            $data['createTime'] = time();
            $data['payment_order_id'] = date('YmdHis').rand(1000,9999);
            $id = model('order')->insertGetId($data);
            //paidan
            $isPandan = Cookie::get('isP');
            if($isPandan && $shop->owner){
                $owner = model('user')->where('uid',$shop->owner)->find();
                if($owner){
                    $array['ugrade'] = $owner->grade;//卖家
                }
                $array['shopId'] = $data['shopId'];
                $array['orderId'] = $id;
                $array['uid'] = $shop->owner;//卖家
                $array['buid'] = $this->uid;//买家
                $array['bgrade'] = $this->user->grade;//买家
                $array['createTime'] = time();
                model('paidan')->insert($array);
            }

            $this->redirect('pay/weixin/pay',['id'=>$id]);
        }
        else{
            die('没有找到商品');
        }
    }



    public function userAddress($id,$sid,$gid){
        $addresses = model('address')->getUserAddress($this->uid);
        $this->assign('addresses',$addresses);
        $this->assign('id',$id);
        $this->assign('sid',$sid);
        $this->assign('gid',$gid);
        return view('userAddress');
    }

    public function addAddress($id,$sid,$gid){
        $country = model('city')->getAllCountry();
        $this->assign('country',$country);
        $this->assign('id',$id);
        $this->assign('sid',$sid);
        $this->assign('gid',$gid);
        return view('addAddress');
    }


    public function ajaxGetAreaByPid(){

        $pid = Request::instance()->get('pid');
        $data = model('city')->getCityByPid($pid);
        return $data;

    }
    public function saveAddress(){

        $data = Request::instance()->post();
        $id = $data['id'];
        $sid = $data['sid'];
        $gid = $data['gid'];
        unset($data['id']);
        unset($data['sid']);
        unset($data['gid']);
        $data['uid'] = $this->uid;
        $data['createTime'] = time();
        model('address')->insert($data);
        $this->redirect('userAddress',['id'=>$id,'sid'=>$sid,'gid'=>$gid]);

    }

    public function address_del(){
        $id = Request::instance()->post('id');
        return model('address')->where('id',$id)->delete();
    }


    public function address_setDefault(){
        $id = Request::instance()->post('id');
        $uid = Request::instance()->post('uid');
        $address = model('address')->where('id',$id)->find();
        if($address->isDefault==0){
            model('address')->where('uid',$uid)->update(['isDefault'=>0]);
            $address->isDefault = 1;
            $address->save();
        }
        else{
            $address->isDefault = 0;
            $address->save();
        }

       return 1;
    }



    public function comment($sid,$pid){
        $comment_type = Config::get('comment_type');
        $comments = model('comment')->getProductComment($pid,$sid);
        $this->assign('comments',$comments);
        $this->assign('id',$pid);
        $this->assign('sid',$sid);
        $this->assign('comment_type',$comment_type);
        return view('comment');
    }



    //代理商品
    public function agentProduct($id){
        $shop = $this->getMyShop();
        $res = model('shop_product')->where('shopId',$shop->id)->where('productId',$id)->find();
        if($res){
            $this->success('成功',url('index/shop/product'));
        }
        else{
            model('shop_product')->insert([
                'shopId'=>$shop->id,
                'productId'=>$id,
            ]);
            $this->success('成功',url('index/shop/product'));
        }

    }

      public function cancelAgent($id){
            $shop = $this->getMyShop();
            $res = model('shop_product')->where('shopId',$shop->id)->where('productId',$id)->delete();
            if($res){
                $this->success('成功',url('index/center/goodsShelves'));
            }

        }



}
